package cn.anecansaitin.whimsy.common.listener;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.register.ModEnchantments;
import net.minecraft.nbt.IntTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.BowItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.AnvilUpdateEvent;

@Mod.EventBusSubscriber(modid = Whimsy.MODID)
public class BowRenameOnAnvilEvent {
    @SubscribeEvent
    public static void onRename(AnvilUpdateEvent event) {
        ItemStack bow = event.getLeft();

        if (!(bow.getItem() instanceof BowItem)) {
            return;
        }

        if (!event.getRight().is(Items.ECHO_SHARD)) {
            return;
        }

        int lvl1 = bow.getEnchantmentLevel(ModEnchantments.ARROW_REST.get());
        int lvl2 = bow.getEnchantmentLevel(ModEnchantments.PHANTASM.get());
        int lvl3 = bow.getEnchantmentLevel(ModEnchantments.AUTOMATIC_TRIGGER.get());

        if (lvl1 < 3 || lvl2 < 1 || lvl3 < 6 || event.getName() == null || !event.getName().equals("A-幻影弓-E")) {
            return;
        }

        ItemStack output = bow.copy();
        event.setOutput(output);
        output.addTagElement("CustomModelData", IntTag.valueOf(1));
        output.setHoverName(Component.literal(event.getName()));
    }
}
